home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / sound / speech recognition sample / mainfunctions.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  6.0 KB  |  342 lines

  1. /*
  2.     File:        MainFunctions.c
  3.  
  4.     Contains:    
  5.  
  6.     Written by:     
  7.  
  8.     Copyright:    Copyright © 1996-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 8/2/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23. #ifndef __MAINFUNC__
  24. #include "MainFunctions.h"
  25. #endif
  26.  
  27. extern     SpeechChannel        theSpeechChan;
  28. extern    Boolean                gDone;
  29.  
  30. /*
  31.     This is a dispatcher for dealing with what the user said.  Add your functions to the switch
  32.     statement, add an entry in the 'LANG' resource, and it should "just work".
  33. */
  34. OSErr        ParseRefCon            (SpeechInfoPtr theSpeechInfo, CommandPtr theCommand)
  35. {
  36.     LanguageModelListPtr    language;
  37.     long                    type            = 0;
  38.     OSErr                    theErr            = noErr;
  39.     short                    word            = 0;
  40.     Boolean                    done            = false,
  41.                             found            = false;
  42.  
  43.     type = theCommand->type;
  44.     word = theCommand->ID;
  45.  
  46.     PlaySndCommand (8192);        /* Play the conformation tones */
  47.  
  48.     switch (type) {
  49.         case 'LANG':
  50.             language = theSpeechInfo->languages;
  51.             while (done == false) {
  52.                 if (language->resID != word) {
  53.                     if (language->nextLanguage != nil) {
  54.                         language = language->nextLanguage;
  55.                     }
  56.                     else {
  57.                         done = true;
  58.                         found = false;
  59.                     }
  60.                 }
  61.                 else {
  62.                     done = true;
  63.                     found = true;
  64.                 }
  65.             }
  66.             if (found == true) {
  67.                 theErr = SRSetLanguageModel (theSpeechInfo->theRecognizer, language->theLanguage);
  68.             }
  69.             break;
  70.         case 'chan':
  71.             theErr = ChannelCommand (word);
  72.             break;
  73.         case 'lght':
  74.             theErr = LightCommand (word);
  75.             break;
  76.         case 'scpt':
  77.             theErr = ScriptCommand (word);
  78.             break;
  79.         case 'call':
  80.             theErr = PhoneCommand (word);
  81.             break;
  82.         case 'game':
  83.             theErr = GameCommand (word);
  84.             break;
  85.         case 'stro':
  86.             theErr = StereoCommand (word);
  87.             break;
  88.         case 'tv  ':
  89.             theErr = TVCommand (word);
  90.             break;
  91.         case 'vcr ':
  92.             theErr = VCRCommand (word);
  93.             break;
  94.         case 'ldsk':
  95.             theErr = LaserdiskCommand (word);
  96.             break;
  97.         case 'vol ':
  98.             theErr = VolumeCommand (word);
  99.             break;
  100.         case 'snd ':
  101.             theErr = PlaySndCommand (word);
  102.             break;
  103.         case 'quit':
  104.             theErr = QuitCommand (word);
  105.             break;
  106.     }
  107.  
  108.     return theErr;
  109. }
  110.  
  111. OSErr    ChannelCommand        (long commandNumber)
  112. {
  113.     OSErr        theErr        = noErr;
  114.  
  115.     switch (commandNumber) {
  116.         case 0:
  117.             break;
  118.         case 1:
  119.             break;
  120.         default:
  121.             ;
  122.     }
  123.  
  124.     return theErr;
  125. }
  126.  
  127. OSErr    LightCommand        (long commandNumber)
  128. {
  129.     OSErr        theErr        = noErr;
  130.  
  131.     switch (commandNumber) {
  132.         case 255:
  133.             break;
  134.         case 25:
  135.             break;
  136.         case 0:
  137.             break;
  138.         default:
  139.             ;
  140.     }
  141.  
  142.     return theErr;
  143. }
  144.  
  145. OSErr    ScriptCommand        (long commandNumber)
  146. {
  147.     ComponentInstance    cmptInstance    = 0;
  148.     AEDesc                desc            = {0, nil};
  149.     OSAID                scriptID        = 0,
  150.                         resultingOSAID    = 0;
  151.     OSErr                theErr            = noErr;
  152.  
  153.     cmptInstance = OpenDefaultComponent (kOSAComponentType, 'scpt');
  154.     if (cmptInstance == nil)
  155.         DebugStr ("\pOpenDefaultComponent failed, do you have AppleScript installed?");
  156.     desc.descriptorType = 'scpt';
  157.  
  158.     desc.dataHandle = Get1Resource ('scpt', commandNumber);
  159.     theErr = ResError ();
  160.     if (desc.dataHandle == nil || theErr != noErr)
  161.         DebugStr ("\pGet1Resource failed");
  162.  
  163.     theErr = OSALoad (cmptInstance, &desc, kOSAModeNull, &scriptID);
  164.     if (theErr != noErr)
  165.         DebugStr ("\pOSALoad failed");
  166.  
  167.     ReleaseResource (desc.dataHandle);
  168.     theErr = OSAExecute (cmptInstance, scriptID, kOSANullScript, kOSAModeNull, &resultingOSAID);
  169.     if (theErr != noErr)
  170.         DebugStr ("\pOSAExecute failed");
  171.  
  172.     theErr = OSADispose (cmptInstance, scriptID);
  173.     if (theErr != noErr)
  174.         DebugStr ("\pOSADispose failed");
  175.  
  176.     theErr = CloseComponent (cmptInstance);
  177.     if (theErr != noErr)
  178.         DebugStr ("\pCloseComponent failed");
  179.  
  180.     return theErr;
  181. }
  182.  
  183. OSErr    PhoneCommand        (long commandNumber)
  184. {
  185.     OSErr        theErr        = noErr;
  186.  
  187.     switch (commandNumber) {
  188.         default:
  189.             ;
  190.     }
  191.  
  192.     return theErr;
  193. }
  194.  
  195. OSErr    GameCommand            (long commandNumber)
  196. {
  197.     OSErr        theErr        = noErr;
  198.  
  199.     switch (commandNumber) {
  200.         default:
  201.             ;
  202.     }
  203.  
  204.     return theErr;
  205. }
  206.  
  207. OSErr    StereoCommand        (long commandNumber)
  208. {
  209.     OSErr        theErr        = noErr;
  210.  
  211.     switch (commandNumber) {
  212.         case 0:
  213.             break;
  214.         case 1:
  215.             break;
  216.         default:
  217.             ;
  218.     }
  219.  
  220.     return theErr;
  221. }
  222.  
  223. OSErr    TVCommand            (long commandNumber)
  224. {
  225.     OSErr        theErr        = noErr;
  226.  
  227.     switch (commandNumber) {
  228.         case 0:
  229.             break;
  230.         case 1:
  231.             break;
  232.         default:
  233.             ;
  234.     }
  235.  
  236.     return theErr;
  237. }
  238.  
  239. OSErr    LaserdiskCommand    (long commandNumber)
  240. {
  241.     OSErr        theErr        = noErr;
  242.  
  243.     switch (commandNumber) {
  244.         case 0:
  245.             break;
  246.         case 1:
  247.             break;
  248.         case 2:
  249.             break;
  250.         case 3:
  251.             break;
  252.         case 4:
  253.             break;
  254.         default:
  255.             ;
  256.     }
  257.  
  258.     return theErr;
  259. }
  260.  
  261. OSErr    VCRCommand            (long commandNumber)
  262. {
  263.     OSErr        theErr        = noErr;
  264.  
  265.     switch (commandNumber) {
  266.         case 0:
  267.             break;
  268.         case 1:
  269.             break;
  270.         case 2:
  271.             break;
  272.         case 3:
  273.             break;
  274.         case 4:
  275.             break;
  276.         case 5:
  277.             break;
  278.         case 6:
  279.             break;
  280.         default:
  281.             ;
  282.     }
  283.  
  284.     return theErr;
  285. }
  286.  
  287. OSErr    VolumeCommand        (long commandNumber)
  288. {
  289.     OSErr        theErr        = noErr;
  290.  
  291.     switch (commandNumber) {
  292.         case 0:
  293.             break;
  294.         case 1:
  295.             break;
  296.         case 2:
  297.             break;
  298.         default:
  299.             ;
  300.     }
  301.  
  302.     return theErr;
  303. }
  304.  
  305. OSErr    PlaySndCommand        (long commandNumber)
  306. {
  307.     Handle        theSound    = nil;
  308.     OSErr        theErr        = noErr;
  309.  
  310.     switch (commandNumber) {
  311.         default:
  312.             theSound = GetResource ('snd ', commandNumber);
  313.             theErr = ResError ();
  314.             HLock (theSound);
  315.             if (theSound != nil && theErr == noErr) {
  316.                 SndPlay (nil, (SndListHandle)theSound, false);
  317.                 HUnlock (theSound);
  318.                 ReleaseResource (theSound);
  319.             }
  320.     }
  321.  
  322.     return theErr;
  323. }
  324.  
  325. OSErr    QuitCommand            (long commandNumber)
  326. {
  327. #pragma unused (commandNumber)
  328.  
  329.     Ptr            goodbye        = "You're welcome, good bye.";
  330.     short        goodbyeLen    = 25;
  331.     OSErr        theErr        = noErr;
  332.  
  333.     theErr = SpeakBuffer (theSpeechChan, (char *)goodbye, goodbyeLen, 0);
  334.     if (theErr != noErr) {
  335.         DebugStr ("\pSpeakBuffer failed");
  336.     }
  337.  
  338.     gDone = true;
  339.  
  340.     return theErr;
  341. }
  342.